# database.py
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession
from sqlalchemy.orm import sessionmaker
from typing import AsyncGenerator

# 创建异步引擎
engine = create_async_engine(
    "sqlite+aiosqlite:///./users.db",  # 可以替换为你的数据库URL
    echo=True,
    future=True
)

# 创建异步会话工厂
async_session = sessionmaker(
    engine, 
    class_=AsyncSession, 
    expire_on_commit=False
)

# 获取数据库会话依赖
async def get_session() -> AsyncGenerator[AsyncSession, None]:
    async with async_session() as session:
        yield session